package org.sample.pattern.proxy.ref;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/** 代理模式使用jdk动态代理实现 */
public class ProxyServiceRefImpl implements InvocationHandler {

    private Object obj;

    public ProxyServiceRefImpl(Object obj) {
        this.obj = obj;
    }

    public static Object newInstance(Object obj) {
        Class<?> clazz = obj.getClass();
        return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), new ProxyServiceRefImpl(obj));
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log(args[0]);
        Object result = method.invoke(obj, args);
        log(result);
        return result;
    }

    public void log(Object message) {
        System.out.println("log:message=" + message);
    }

}